#include "BondDetailDlg.h"

#include <bondlib/BondContainer.h>
#include <core/StyleMgr.h>
#include <core/Utility.h>
#include <qbprotocol/include/SSIDBModel.h>
#include <uam/UserAccountManager.h>

#include <QVBoxLayout>

#include "../QbMessageProcess.h"
#include "BondDetailBroker.h"
#include "BondDetailCfets.h"
#include "BondDetailChart.h"
#include "BondDetailData.h"
#include "BondDetailExchange.h"
#include "BondDetailTab.h"
#include "BondDetailTitle.h"
#include "ui_BondDetailPage.h"

BondDetailDlg::BondDetailDlg(QWidget* parent) : BondDetailPage(parent) {
  m_data.reset(new DetailData(this));

  // m_brokerDlg = ui->BrokerPage;
  // m_cfetsDlg = ui->CFETSPage;
  // m_exDlg = ui->ExchangePage;
}

BondDetailDlg::~BondDetailDlg() {}

bool BondDetailDlg::setCombondKey(const char* bondKey /*= nullptr*/,
                                  int brokerId /*= -1*/) {
  std::string prevBondKey = m_data->comBondKey();
  if (prevBondKey == bondKey) return true;

  int bondIndex = CBondContainer::instance().GetBondIndex(bondKey);
  if (!CBondContainer::instance().IsValidIndex(bondIndex)) return false;
  // const CBondInfo& bondInfo =
  // CBondContainer::instance().ElementAtR(bondIndex); setupUI(bondInfo,
  // brokerId);
  m_data->setCombondKey(bondKey, brokerId);

  setDetailData(m_data);

  return true;
}

bool BondDetailDlg::setBroker(int nBrokerId) {
  return false;  // m_brokerDlg->setBrokerTab(nBrokerId);
}

#define REMOVE_WIDGET(W)            \
  if (W) {                          \
    m_quoteLayout->removeWidget(W); \
    W->setParent(nullptr);          \
    delete W;                       \
    W = nullptr;                    \
  }

void BondDetailDlg::setupUI(const CBondInfo& pbi, int brokerId) {
  const CBondInfo& prevBi = m_data->bond();

  //初始创建
  if (m_data->comBondKey().empty()) {
    QVBoxLayout* mainLayout = new QVBoxLayout();
    mainLayout->setDirection(QVBoxLayout::Down);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(StyleMgr::instance().size("CtrlSpacing"));
    setLayout(mainLayout);

    BondDetailTitle* title = new BondDetailTitle(this);  //, m_data
    title->setFixedHeight(96);
    mainLayout->addWidget(title);

    m_tab = new BondDetailTab(this, m_data);
    m_tab->setFixedHeight(24);
    connect(m_tab, &BondDetailTab::tabSelected, this,
            &BondDetailDlg::onTabSelChanged);
    mainLayout->addWidget(m_tab);

    m_quoteLayout = new QHBoxLayout();
    m_quoteLayout->setContentsMargins(0, 0, 0, 0);
    m_quoteLayout->setSpacing(4);
    mainLayout->addLayout(m_quoteLayout);

  } else if (m_data->bond().GetMarketType() !=
             pbi.GetMarketType())  //当前页面债券改变，市场类型改变
  {
    if (atoi(m_data->bond().GetMarketType()) == 2)  //原来是二级市场
    {
      REMOVE_WIDGET(m_brokerDlg);
      REMOVE_WIDGET(m_cfetsDlg);
      REMOVE_WIDGET(m_exDlg);
      REMOVE_WIDGET(m_chart);
    } else {
    }
  }

  if (atoi(pbi.GetMarketType()) == 2)  // 2级
  {
    setupSecondaryUI(brokerId);
  } else  // 1级
  {
    setupPrimaryUI();
  }
}

void BondDetailDlg::setupSecondaryUI(int brokerId) {
  if (brokerId == -1 && !m_brokerDlg) {
    m_brokerDlg = new BondDetailBroker(this);  //, m_data
    m_brokerDlg->setMinimumWidth(480);
    m_quoteLayout->addWidget(m_brokerDlg, 2);
  } else if (brokerId == NEW_DETAIL_CFETS_BROKER && !m_cfetsDlg) {
    m_cfetsDlg = new BondDetailCFETS(this);  //, m_data
    m_cfetsDlg->setMinimumWidth(480);
    m_quoteLayout->addWidget(m_cfetsDlg, 2);
    m_tab->setSelIndex(1);
    m_secondeDlg = m_cfetsDlg;
  } else if (brokerId == NEW_DETAIL_EXCHANGE_BROKER && !m_exDlg) {
    m_exDlg = new BondDetailExchange(this);  //, m_data
    m_exDlg->setMinimumWidth(480);
    m_quoteLayout->addWidget(m_exDlg, 2);
    m_tab->setSelIndex(1);
    m_secondeDlg = m_exDlg;
  }

  if (!m_chart) {
    m_chart = new BondDetailChart(this);  //, m_data
    m_quoteLayout->addWidget(m_chart, 3);
  }
}

void BondDetailDlg::setupPrimaryUI() {
  m_primaryDlg = new BondDetailPrimary(this, m_data);
  m_primaryDlg->setMinimumSize(1020, 490);
  m_quoteLayout->addWidget(m_primaryDlg, 2);
}

void BondDetailDlg::onTabSelChanged(int brokerid) {
  switch (brokerid) {
    case -1:  //经济商
    {
      if (m_brokerDlg == nullptr) {
        m_brokerDlg = new BondDetailBroker(this);  //, m_data
        m_brokerDlg->setMinimumWidth(480);
        m_quoteLayout->insertWidget(0, m_brokerDlg, 2);
      }
      m_brokerDlg->show();
      if (m_secondeDlg) m_secondeDlg->hide();
    } break;
    case NEW_DETAIL_CFETS_BROKER: {
      if (m_secondeDlg && m_secondeDlg == m_exDlg)  // listedmarket改变
      {
        REMOVE_WIDGET(m_exDlg);
      }
      if (m_cfetsDlg == nullptr) {
        m_cfetsDlg = new BondDetailCFETS(this);  //, m_data
        m_cfetsDlg->setMinimumWidth(480);
        m_quoteLayout->insertWidget(1, m_cfetsDlg, 2);
        m_secondeDlg = m_cfetsDlg;
        m_data->reqCfetsMarketStream(0);
      }
      m_brokerDlg->hide();
      m_secondeDlg->show();
    } break;
    case NEW_DETAIL_EXCHANGE_BROKER: {
      if (m_secondeDlg && m_secondeDlg == m_cfetsDlg) {
        REMOVE_WIDGET(m_cfetsDlg);
      }
      if (m_exDlg == nullptr) {
        m_exDlg = new BondDetailExchange(this);  //, m_data
        m_exDlg->setMinimumWidth(480);
        m_quoteLayout->insertWidget(1, m_exDlg, 2);
        m_secondeDlg = m_exDlg;
      }
      m_brokerDlg->hide();
      m_secondeDlg->show();
    } break;
    default:
      break;
  }
}
